package com.dang.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.dang.dao.UserDAO;
import com.dang.model.User;
import com.dang.util.DBConnection;
	
public class UserDAOImp implements UserDAO{
	
	private static final String INSERT= "insert into d_user(email,nickname,password,user_integral,is_email_verify,email_verify_code,last_login_time,last_login_ip) values(?,?,?,?,?,?,?,?)";
	private static final String FINDBYEMAIL= "select * from d_user where email=?";
	private static final String FINDBYID="select * from d_user where id=?";
	private static final String UPDATE= "update   d_user   set email=?,nickname=?,password=?,user_integral=?,is_email_verify=?,email_verify_code=?,last_login_time=?,last_login_ip=? where id=?";
	public User findByEmail(String email) throws Exception {
		PreparedStatement prep= null;
		Connection conn= null;
		User u= null;
		ResultSet rst= null;
		try{
			conn= DBConnection.getConnection();
			prep= conn.prepareStatement(FINDBYEMAIL);
			prep.setString(1, email);
			rst= prep.executeQuery();
		if(rst.next()) {
		u= new User();
		u.setEmail(rst.getString("email"));
		u.setEmailVerifyCode(rst.getString("email_verify_code"));
		u.setId(rst.getInt("id"));
		u.setLastLoginip(rst.getString("last_login_ip"));
		u.setLastLoginTime(rst.getLong("last_login_time"));
		u.setNickname(rst.getString("nickname"));
		u.setPassword(rst.getString("password"));
		u.setUserInergral(rst.getInt("user_integral"));
		String is_email_verify= rst.getString("is_email_verify");
		if(is_email_verify.equals("T")) {
			u.setEmailVerify(true);
			} 
		else {
			u.setEmailVerify(false);
		}
	}
		} finally {
			DBConnection.close(prep, conn);
		}
		return u;
	}
	public User findById(int id) throws Exception {
		PreparedStatement prep= null;
		Connection conn= null;
		User u= null;
		try{
			conn= DBConnection.getConnection();
			prep= conn.prepareStatement(FINDBYID);
			prep.setInt(1, id);
			ResultSet rst= prep.executeQuery();
			if(rst.next()) {
			u= new User();
			u.setEmail(rst.getString("email"));
			u.setEmailVerifyCode(rst.getString("email_verify_code"));
			u.setId(rst.getInt("id"));
			u.setLastLoginip(rst.getString("last_login_ip"));
			u.setLastLoginTime(rst.getLong("last_login_time"));
			u.setNickname(rst.getString("nickname"));
			u.setPassword(rst.getString("password"));
			u.setUserInergral(rst.getInt("user_integral"));
			String is_email_verify= rst.getString("is_email_verify");
			if(is_email_verify.equals("T")) {
				u.setEmailVerify(true);
				} else{
					u.setEmailVerify(false);
					}
			}
			} finally{
				DBConnection.close(prep, conn);
				}
			return u;
			}
	public void save(User user) throws Exception {
		PreparedStatement prep= null;
		Connection conn= null;
		try{conn= DBConnection.getConnection();
		prep= conn.prepareStatement(INSERT,Statement.RETURN_GENERATED_KEYS);
		prep.setString(1, user.getEmail());prep.setString(2, user.getNickname());
		prep.setString(3, user.getPassword());
		prep.setInt(4, user.getUserInergral());
		if(user.isEmailVerify()) {
			prep.setString(5, "T");
			} else{
				prep.setString(5, "F");
				}
		prep.setString(6, user.getEmailVerifyCode());
		prep.setLong(7, user.getLastLoginTime());
		prep.setString(8, user.getLastLoginip());
		prep.executeUpdate();
		ResultSet rst= prep.getGeneratedKeys();
		rst.next();
		int id= rst.getInt(1);user.setId(id);
		} finally{
			DBConnection.close(prep, conn);
		}
		}
	public void update(User u) throws Exception {
		PreparedStatement prep= null;
		Connection conn= null;
				try{
					conn= DBConnection.getConnection();
					prep= conn.prepareStatement(UPDATE);
					prep.setString(1, u.getEmail());
					prep.setString(2, u.getNickname());
					prep.setString(3, u.getPassword());
					prep.setInt(4, u.getUserInergral());
					if(u.isEmailVerify()) {
						prep.setString(5, "T");
						} else{
							prep.setString(5, "F");
							}
					prep.setString(6, u.getEmailVerifyCode());
					prep.setLong(7, u.getLastLoginTime());
					prep.setString(8, u.getLastLoginip());
					prep.setInt(9, u.getId());
					prep.executeUpdate();
					} finally{
						DBConnection.close(prep, conn);
						}
		}
}
					
						
					
				